package com.iflytek.uoamp.agent.monitor.sender;

import java.util.List;

import com.iflytek.uoamp.agent.common.constants.CommonConstants;
import org.apache.log4j.Logger;

import com.iflytek.uoamp.agent.core.conf.AgentCoreConfig;
import com.iflytek.uoamp.agent.core.sender.monitorsender.monitorinterface.IItem;
import com.iflytek.uoamp.agent.monitor.mcagent.MCAgentConfig;
import com.iflytek.uoamp.agent.monitor.sender.biz.AbsSender;
import com.iflytek.uoamp.agent.monitor.sender.biz.http.HttpSender;
import com.iflytek.uoamp.agent.monitor.sender.biz.quick.QuickSender;
import com.iflytek.uoamp.agent.monitor.sender.biz.safe.SafeSender;
import com.iflytek.uoamp.agent.monitor.sender.config.Config;
import com.iflytek.uoamp.agent.monitor.sender.interfaces.ISenderFactory;
import com.iflytek.uoamp.agent.monitor.sender.loadbalance.LoadbalanceManager;
import com.iflytek.uoamp.agent.monitor.sender.schedual.ScheduleNoSend;
import com.iflytek.uoamp.agent.monitor.utils.PropertiesHelper;

public class MsgSendClient {

	private AbsSender safeSender;
	private AbsSender quickSender;
	private AbsSender httpSender;
	private AbsSender sender;
	private ScheduleNoSend scheduleNoSend;
	private LoadbalanceManager lbManager;
	private Config config;
	private Thread thread = null;
	
	@SuppressWarnings("rawtypes")
	public MsgSendClient(ISenderFactory factory) {
		boolean isLinux = AgentCoreConfig.getInstance().isLinux();
		String monitorConfPath = "";
    	if(isLinux){
    		monitorConfPath = CommonConstants.LINUX_MONITOR_PATH;
    		Logger.getRootLogger().info("use linux conf monitorMonitor.properties!");
    	}else{
    		monitorConfPath = CommonConstants.WIN_MONITOR_PATH;
    		Logger.getRootLogger().info("use windows conf monitorMonitor.properties!");
    	}
		Config config = PropertiesHelper.getCientConfig(monitorConfPath);
		this.config = config;
		if(!config.isUseHttp()){
			this.lbManager = new LoadbalanceManager(factory, config);
			this.thread = new Thread(this.lbManager,"LoadbalanceManager Thread");
			this.safeSender = new SafeSender(this.lbManager, factory, config);
			this.quickSender = new QuickSender(this.lbManager, factory, config);
			
			if (config.isUseSafeMod()) {
				this.sender = safeSender;
			} else {
				this.sender = quickSender;
			}
			
			if (config.getSchedualIntervalTime() != -1) {
				scheduleNoSend = new ScheduleNoSend(sender);
				scheduleNoSend.addOneTask();
			}
			
		}else{
			httpSender = new HttpSender(config);
			sender = httpSender;
		}
		
	}

	public void start(){
		if(thread!=null){
			this.thread.start();
		}
	}
	
	public Config getConfig() {
		return config;
	}

	public void setConfig(Config config) {
		this.config = config;
	}

	public void send(IItem item) {
		if(config.isUseHttp()){
			httpSender.send(item);
		}else{
			sender.send(item);
		}
		
	}

	public void sendBatch(List<IItem> item) {
		if(config.isUseHttp()){
			httpSender.sendBatch(item);
		}else{
			sender.sendBatch(item);
		}
	}

	public Object sendBySafe(IItem item) {
		return safeSender.send(item);
	}

	public Object sendBatchBySafe(List<IItem> item) {
		return safeSender.sendBatch(item);
	}

	public void sendByQucik(IItem item) {
		quickSender.send(item);
	}

	public void sendBatchByQucik(List<IItem> item) {
		quickSender.sendBatch(item);
	}
	
	public Object sendByHttp(IItem item) {
		return httpSender.send(item);
	}

	public Object sendBatchByHttp(List<IItem> item) {
		return httpSender.sendBatch(item);
	}
}
